package com.bxait.toymail.coupon.service.impl;

import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.Query;
import com.bxait.toymail.coupon.entity.SeckillSkuRelationEntity;
import com.bxait.toymail.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bxait.toymail.coupon.dao.SeckillSessionDao;
import com.bxait.toymail.coupon.entity.SeckillSessionEntity;
import com.bxait.toymail.coupon.service.SeckillSessionService;
import javax.annotation.Resource;

@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {


    @Resource
    @Qualifier("seckillSkuRelationService")
    SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /*
     * @param 
     * @==============History===============<br/>
     * @Description //   获取最近3天需要上架的秒杀商品
     * @Date 2023/4/30 2023/4/30 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return java.util.List<com.bxait.toymail.coupon.entity.SeckillSessionEntity>
     */
    @Override
    public List<SeckillSessionEntity> getLate3DaySession() {

        // 获取最近3天的活动场次
        List<SeckillSessionEntity> list = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", startTime(), endTime()));
        // 封装活动中关联的商品信息
        if(list != null && list.size() > 0){
             List<SeckillSessionEntity> collect = list.stream().map(item -> {
                 Long id = item.getId();
                 List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id));
                 item.setRelationSkus(relationEntities);
                return item;
            }).collect(Collectors.toList());

             return collect;
        }
        return null;
    }



    /*
     * @param 
     * @==============History===============<br/>
     * @Description //   获取当天的开始时间
     * @Date 2023/4/30 2023/4/30 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return java.lang.String
     */
    private String startTime() {
        // 获取当前时间
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        // 获取当前最小时间
        LocalDateTime start = LocalDateTime.of(now, min);

        String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }


    /*
     * @param 
     * @==============History===============<br/>
     * @Description //   获取3天后的最大结束时间
     * @Date 2023/4/30 2023/4/30 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return java.lang.String
     */
    private String endTime() {
        // 获取3天后的最大时间
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.plusDays(2);
        LocalDateTime of = LocalDateTime.of(localDate, LocalTime.MAX);
        String format = of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }


}